//
//  XZAccoutnTool.m
//  XZIBeeBo
//
//  Created by Sean on 16/4/7.
//  Copyright © 2016年 XZSean. All rights reserved.
//

#define XZPATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Access.archiver"]


#import "XZAccoutnTool.h"
#import "XZAccoutnModel.h"


@implementation XZAccoutnTool


+ (NSString *)path{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //获取到文件
    NSString *accessDataPata =  [document stringByAppendingPathComponent:@"Access.archiver"];

    return accessDataPata;
}
/**
 *  归档accoun模型对象
 */
+(void)saveAccoutnModel:(XZAccoutnModel *)accoutnModel{
   [NSKeyedArchiver archiveRootObject:accoutnModel toFile:self.path];

}
/**
 *  解档account模型  取出数据
 */
+ (XZAccoutnModel *)account{
   
     XZAccoutnModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:self.path];
    
    //验证账号是否过期
    // 过期的秒数
    long long expires_in = [account.expires_in longLongValue];
    // 获得过期时间
    NSDate *expiresTime = [account.created_time dateByAddingTimeInterval:expires_in];
    // 获得当前时间
    NSDate *now = [NSDate date];
    
    // 如果expiresTime <= now，过期
    /**
     NSOrderedAscending = -1L, 升序，右边 > 左边
     NSOrderedSame,            一样
     NSOrderedDescending       降序，右边 < 左边
     */
    NSComparisonResult result = [expiresTime compare:now];
    if (result != NSOrderedDescending) { // 过期
        return nil;
    }
    
    return account;
}

@end
